<?php

namespace app\teacher\controller\student;

use app\common\controller\Frontend;
use app\common\enums\say\SayCategoryTypeEnum;
use app\common\model\banxi\say\SayGrade as SayGradeModel;
use app\common\model\banxi\say\SayTeachSentence as SayTeachSentenceModel;

class StudentTeachSentence extends Frontend
{
    protected array $noNeedLogin = [];

    protected array $noNeedPermission = ['*'];

    public function initialize(): void
    {
        parent::initialize();
    }

    /**
     * 查询学员开通课程(不包括体验课)
     * @return array 课程列表
     */
    public function listStudentOpen()
    {
        $param = $this->request->filter('trim')->param(['index', 'limit', 'orderBy', "query", "isPractice", "studentId"]);
        if(!isset($param['query']['EQ_parentId'])){
            $param['query']['EQ_parentId'] = 0;
        }
        //查询条件解析
        $where = get_query($param);
        //获取分页数据
        $pageParams = get_page_params($param);
        $sayTeachSentenceModel = new SayTeachSentenceModel();
        $list = $sayTeachSentenceModel->getListStudentOpenPage($param['isPractice'], $param['studentId'], $where, $pageParams['page'], $pageParams['limit']);
        $this->ok('操作成功', result_row($list));
    }

    /**
     * 查询学员开通课程(包括体验课)
     * @return array 课程列表
     */
    public function listStudentOpenTrialClass()
    {
        $studentId = $this->request->param('studentId');
        $studentId = $this->request->param('courseFullType');
        if (!$studentId) {
            $this->fail('请选择学生', []);
        }
        $teachSentence = new SayTeachSentenceModel();
        //开通课程
        $mainClass = $teachSentence->alias('a')->whereExists(function ($query) use ($studentId) {
            $query->name('banxi_student_course')
                ->whereColumn('id', '=', 'a.gradeId')
                ->where(['studentId' => $studentId])
                ->where('del', 0);
        })->field('id,gradeId,sentenceGroupEn,sentenceGroupZh,name')->select()->toArray();

        //体验课
        $trialClassList = $teachSentence->alias('a')->whereExists(function ($query) {
            $query->name('banxi_say_grade')
                ->alias('b')
                ->whereColumn('id', '=', 'a.gradeId')
                ->whereExists(function ($query) {
                    $query->name('banxi_say_category')
                        ->whereColumn('id', '=', 'b.sayCategoryId')
                        ->where('type', '=', SayCategoryTypeEnum::E3)
                        ->where('del', 0);
                });
        })->field('id,gradeId,sentenceGroupEn,sentenceGroupZh,name')->select()->toArray();
        $list = array_merge($mainClass, $trialClassList);
        $gradeModel = new SayGradeModel();
        $gradeList = $gradeModel->column('id,name', 'id');
        // 重构数据组织逻辑
        $result = [];
        foreach ($gradeList as $gradeId => $gradeInfo) {
            $gradeInfo['teachSentenceList'] = [];
            $result[$gradeId] = $gradeInfo;
        }

        foreach ($list as $item) {
            if (isset($result[$item['gradeId']])) {
                $result[$item['gradeId']]['teachSentenceList'][] = $item;
            }
        }
        $this->ok('ok', array_values($result));
    }


}